package com.sls.nzdapplication.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;


import com.bin.david.form.core.SmartTable;
import com.bin.david.form.data.column.Column;
import com.bin.david.form.data.style.FontStyle;
import com.bin.david.form.data.table.TableData;
import com.sls.nzdapplication.R;
import com.sls.nzdapplication.db.utils.DyEquipmentUtil;
import com.sls.nzdapplication.db.utils.MeterBoxUtil;
import com.sls.nzdapplication.model.DyEquipment;
import com.sls.nzdapplication.model.DyManagementBoxData;
import com.sls.nzdapplication.model.MeterBox;
import com.sls.nzdapplication.ui.activity.MeterBoxActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 这个类是 DyManagementBoxFragment，它是一个 Fragment，用于显示电压等级管理箱的信息。
 * 在这个类中，我们使用了 SmartTable 来展示数据，同时还使用了 DyEquipmentUtil 和 MeterBoxUtil 来查询数据。
 * 在 onViewCreated 方法中，我们初始化了 SmartTable，并在 initData 方法中查询了数据并展示了数据。
 * 在 showData 方法中，我们将查询到的数据转换成了 DyManagementBoxData 类型的数据，并将其展示在了 SmartTable 中。
 * 在 onResume 方法中，我们重新查询了数据并展示了数据。
 * */
public class DyManagementBoxFragment extends Fragment {

    private MeterBoxUtil meterBoxUtil;

    private LinearLayout dataView;

    private DyEquipmentUtil dyEquipmentUtil;

    private List<MeterBox> meterBoxList;

    private String lineId;

    private SmartTable<DyManagementBoxData> table;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.dy_box_manage_layout, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        lineId = getArguments().getString("areaId");
        table = view.findViewById(R.id.table);
        table.getConfig().setShowXSequence(false);
        table.getConfig().setShowYSequence(false);
        table.getConfig().setShowTableTitle(false);
        table.getConfig().setColumnTitleStyle (new FontStyle(30, getResources().getColor(R.color.colorPrimaryDark)));
        table.getConfig().setMinTableWidth(getActivity().getWindowManager().getDefaultDisplay().getWidth());
        initData();

    }

    private void initData(){
        dyEquipmentUtil = new DyEquipmentUtil(getContext());
        meterBoxUtil = new MeterBoxUtil(getContext());
        meterBoxList = meterBoxUtil.queryMeterBoxByLineId(lineId);
        showData(meterBoxList);
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    /**
     * 数据
     */
    private void showData(List<MeterBox> titleData) {
        table.setTableData(null);
        List<DyManagementBoxData> dyManagementBoxDataList = new ArrayList<>();
        for (int i = 0; i < titleData.size(); i++) {
            final MeterBox meterBox = titleData.get(i);
            DyManagementBoxData dyManagementBoxData = new DyManagementBoxData();
            dyManagementBoxData.setBm(meterBox.getBoxCode());
            if(meterBox.getPreviousEquipmentId()!=null){
                DyEquipment dyEquipment = dyEquipmentUtil.listOneDyEquipment(meterBox.getPreviousEquipmentId());
                if(null!=dyEquipment){
                    dyManagementBoxData.setQdsb(dyEquipment.getName());
                }else{
                    dyManagementBoxData.setQdsb("");
                }
            }else{
                dyManagementBoxData.setQdsb("");
            }
            dyManagementBoxData.setCol(String.valueOf(meterBox.getColumn()));
            dyManagementBoxData.setRow(String.valueOf(meterBox.getRow()));
            dyManagementBoxData.setCheck("存在");
            dyManagementBoxDataList.add(dyManagementBoxData);

        }

        if(dyManagementBoxDataList.size()>0){
            table.setVisibility(View.VISIBLE);
            table.setData(dyManagementBoxDataList);
            table.getTableData().setOnRowClickListener(new TableData.OnRowClickListener<DyManagementBoxData>() {
                @Override
                public void onClick(Column column, DyManagementBoxData dyManagementBoxData, int col, int row) {
                    Intent intent = new Intent(getActivity(), MeterBoxActivity.class);
                    intent.putExtra("equipmentId",titleData.get(row).getId());
                    startActivity(intent);
                }
            });
        }else{
            table.setVisibility(View.GONE);
        }

    }
}
